//
//  WQNetTool.m
//  sanwuWeiboOC
//
//  Created by 王先强 on 15/12/8.
//  Copyright © 2015年 王先强. All rights reserved.
//

#import "WQNetTool.h"
#import "WQAccount.h"


///  OAuth认证的请求参数
NSString const *appKey = @"2187107614";
NSString const *appSecret = @"0ce577c8c22561010c54d3a39b26f1a7";
NSString const *redirectUrl = @"http://www.baidu.com";

@implementation WQNetTool
///  OAuth认证的请求
-(NSURLRequest *)oauthURLRequest{
    NSString *urlStr = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",appKey,redirectUrl];
    NSURL *url = [NSURL URLWithString:urlStr];
    NSURLRequest *oauthURLRequest = [NSURLRequest requestWithURL:url];
    return oauthURLRequest;
}
///  OAuth加载令牌accessToken
-(void)loadAccessTokenWithCode:(NSString *)code finishedBlock:(void (^)(id, NSError *))finished{
    NSString *urlString = @"https://api.weibo.com/oauth2/access_token";
    NSDictionary *prama = @{@"client_id":appKey,
                            @"client_secret":appSecret,
                            @"grant_type": @"authorization_code",
                            @"code":code,
                            @"redirect_uri":redirectUrl
                            };
    [self httpMethodType:WQHttpMethodTypePOST prama:prama urlString:urlString result:finished];
    
}
///OAuth 用 access_token加载用户信息
-(void)loadUserInfoWithAccount:(WQAccount *)account finishedBlock:(void(^)(id success, NSError *failure))finished{
    
    NSString *urlString = @"https://api.weibo.com/2/users/show.json";
    NSDictionary *prama = @{@"access_token":account.access_token,
                            @"uid":account.uid
                            };
    [self httpMethodType:WQHttpMethodTypeGET prama:prama urlString:urlString result:finished];
}


///  OAuth网络方法封装
-(void)httpMethodType:(WQHttpMethodType)type prama:(NSDictionary *)prama  urlString:(NSString *)urlString result:(void (^)(id, NSError *))result{
    
    void (^successBlock)(NSURLSessionDataTask*, id) = ^(NSURLSessionDataTask *task, id responseObject){
        result(responseObject,nil);
    };
    void (^errorBlock)(id, NSError *) = ^(NSURLSessionDataTask *task, NSError *error){
        result(nil,error);
    };
    
    
    switch (type) {
        case WQHttpMethodTypeGET:
            [self GET:urlString parameters:prama success:successBlock failure:errorBlock];
            break;
            case WQHttpMethodTypePOST:
            [self POST:urlString parameters:prama success:successBlock failure:errorBlock];
            break;
            
        default:
            break;
    }
}





#pragma mark - /************** 严谨的单例 **************/

static WQNetTool *_instance;
//
//+ (instancetype)shareNetTool
//{
//    return [[self alloc] init];
//}
//
//-(instancetype)init{
//    
//    if (self = [super init]) {
//        NSMutableSet *set = [NSMutableSet setWithSet:_instance.responseSerializer.acceptableContentTypes];
//        [set addObject:@"text/plain"];
//        _instance.responseSerializer.acceptableContentTypes = set;
//    }
//    return _instance;
//}
//
//// 真正分配内存的方法
//+ (instancetype)allocWithZone:(struct _NSZone *)zone
//{
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        _instance = [super allocWithZone:zone];
//    
//
//    });
//    return _instance;
//}


+ (instancetype)shareNetTool
{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        _instance = [[super alloc] init];
        NSMutableSet *set = [NSMutableSet setWithSet:_instance.responseSerializer.acceptableContentTypes];
        [set addObject:@"text/plain"];
        _instance.responseSerializer.acceptableContentTypes = set;
    });
    
    return _instance;
}

// 真正分配内存的方法
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });
    return _instance;
}

// copy时也保证单例
- (id)copyWithZone:(nullable NSZone *)zone
{
    return self;
}
- (id)mutableCopyWithZone:(nullable NSZone *)zone
{
    return self;
}
@end
